package lib.quasar.widget.radius;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import lib.quasar.util.LogUtil;
import lib.quasar.widget.R;

/**
 * description: 圆角
 * created by kalu on 2018/6/1 17:45
 */
public final class RadiusRelativeLayout extends RelativeLayout {

    private final int[][] mState = new int[][]{
            new int[]{android.R.attr.state_pressed},
            new int[]{android.R.attr.state_enabled}
    };

    private final StateListDrawable mBackgroundDrawable = new StateListDrawable();
    private final GradientDrawable mNormalDrawable = new GradientDrawable();
    private final GradientDrawable mPressDrawable = new GradientDrawable();

    public RadiusRelativeLayout(Context context) {
        this(context, null, 0);
    }

    public RadiusRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadiusRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setClickable(true);

        //获取所有的自定义属性
        TypedArray a = null;
        try {
            a = context.obtainStyledAttributes(attrs, R.styleable.RadiusRelativeLayout);

            final float radius = a.getDimension(R.styleable.RadiusRelativeLayout_rrl_bg_radius, 0);
            final float stock = a.getDimension(R.styleable.RadiusRelativeLayout_rrl_stock_size, 0);

            final int color1 = a.getColor(R.styleable.RadiusRelativeLayout_rrl_bg_color_normal, Color.TRANSPARENT);
            final int color2 = a.getColor(R.styleable.RadiusRelativeLayout_rrl_stock_color_normal, Color.TRANSPARENT);
            mNormalDrawable.setColor(color1);
            mNormalDrawable.setCornerRadius(radius);
            mNormalDrawable.setStroke((int) stock, color2);

            final int color3 = a.getColor(R.styleable.RadiusRelativeLayout_rrl_bg_color_press, Color.TRANSPARENT);
            final int color4 = a.getColor(R.styleable.RadiusRelativeLayout_rrl_stock_color_press, Color.TRANSPARENT);
            mPressDrawable.setColor(color3);
            mPressDrawable.setCornerRadius(radius);
            mPressDrawable.setStroke((int) stock, color4);

        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
        } finally {
            if (null == a) return;
            a.recycle();
        }
    }

    @Override
    protected void onFinishInflate() {

        mBackgroundDrawable.addState(mState[0], mPressDrawable);
        mBackgroundDrawable.addState(mState[1], mNormalDrawable);
        mBackgroundDrawable.setEnterFadeDuration(50);
        mBackgroundDrawable.setExitFadeDuration(50);
        setBackgroundDrawable(mBackgroundDrawable);

        super.onFinishInflate();
    }

    public final void setNormalBackgroundColor(int color){
        mNormalDrawable.setColor(color);
    }

    public final void setPressBackgroundColor(int color){
        mPressDrawable.setColor(color);
    }

    public final void setRadius(int radius){
        mNormalDrawable.setCornerRadius(radius);
        mPressDrawable.setCornerRadius(radius);
    }
}